package CinemaDevelop;
import java.time.LocalDateTime;
import java.util.Scanner;

public class CinemaCLT {
    private static Cinema cinema;
    private static User currentUser;
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        initializeCinema();
        showMainMenu();
    }

    private static void initializeCinema() {
        System.out.println("初始化电影院系统...");
        cinema = new Cinema("王二狗的电影院");
        System.out.println("系统初始化完成！");
    }

    private static void showMainMenu() {
        while (true) {
            System.out.println("=== 电影院管理系统 ===");
            System.out.println("1. 登录");
            System.out.println("2. 退出系统");
            System.out.print("请选择: ");

            int choice = readIntInput();

            switch (choice) {
                case 1:
                    login();
                    break;
                case 2:
                    System.out.println("感谢使用，再见！");
                    System.exit(0);
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void login() {
        System.out.println("=== 用户登录 ===");
        System.out.print("用户名: ");
        String username = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();

        currentUser = CinemaDAO.getUserByUsername(username);

        if (currentUser != null) {
            currentUser.login(username, password);
            showUserMenu();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    private static void showUserMenu() {
        while (true) {
            System.out.println("=== 主菜单 ===");
            System.out.println("当前用户: " + currentUser.userName);

            if (currentUser instanceof Administrator) {
                showAdminMenu();
            } else if (currentUser instanceof Manager) {
                showManagerMenu();
            } else if (currentUser instanceof TicketSeller) {
                showSellerMenu();
            }

            System.out.println("0. 退出登录");
            System.out.print("请选择: ");

            int choice = readIntInput();

            if (choice == 0) {
                currentUser.logout();
                currentUser = null;
                return;
            }

            handleUserChoice(choice);
        }
    }

    private static void showAdminMenu() {
        System.out.println("1. 安排放映厅");
        System.out.println("2. 添加电影排期");
        System.out.println("3. 删除电影排期");
        System.out.println("4. 查看所有电影排期");
        System.out.println("5. 查询特定电影");
        System.out.println("6. 售票");
    }

    private static void showManagerMenu() {
        System.out.println("1. 安排放映厅");
        System.out.println("2. 添加电影排期");
        System.out.println("3. 删除电影排期");
        System.out.println("4. 查看所有电影排期");
        System.out.println("5. 查询特定电影");
        System.out.println("6. 售票");
    }

    private static void showSellerMenu() {
        System.out.println("1. 安排放映厅");
        System.out.println("2. 添加电影排期");
        System.out.println("3. 删除电影排期");
        System.out.println("4. 查看所有电影排期");
        System.out.println("5. 查询特定电影");
        System.out.println("6. 售票");
    }

    private static void handleUserChoice(int choice) {
        if (currentUser instanceof Administrator) {
            handleAdminChoice(choice);
        } else if (currentUser instanceof Manager) {
            handleManagerChoice(choice);
        } else if (currentUser instanceof TicketSeller) {
            handleSellerChoice(choice);
        }
    }

    private static void handleAdminChoice(int choice) {
        switch (choice) {
            case 1:
                addScreenRoom();
                break;
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                System.out.println(currentUser.userName + "无权限进行该操作！");
                break;
            default:
                System.out.println("无效选择！");
        }
    }

    private static void handleManagerChoice(int choice) {
        switch (choice) {
            case 2:
                addMovieSchedule();
                break;
            case 3:
                removeMovieSchedule();
                break;
            case 1:
            case 4:
            case 5:
            case 6:
                System.out.println(currentUser.userName + "无权限进行该操作！");
                break;
            default:
                System.out.println("无效选择！");
        }
    }

    private static void handleSellerChoice(int choice) {
        switch (choice) {
            case 1:
            case 2:
            case 3:
                System.out.println(currentUser.userName + "无权限进行该操作！");
                break;
            case 4:
                currentUser.showMovieCasted(cinema);
                break;
            case 5:
                findMovie();
                break;
            case 6:
                sellTicket();
                break;
            default:
                System.out.println("无效选择！");
        }
    }

    private static void addScreenRoom() {
        System.out.println("=== 添加放映厅 ===");
        System.out.print("放映厅(白银厅/黄金厅/翡翠厅/钻石厅/宝石厅): ");
        String roomName = scanner.nextLine();
        currentUser.setScreenRoom(cinema, new ScreenRoom(roomName));
        System.out.println("已成功添加" + roomName);
    }

    private static void addMovieSchedule() {
        System.out.println("=== 添加电影排期 ===");
        System.out.print("电影名称: ");
        String movieName = scanner.nextLine();
        System.out.print("放映厅(白银厅/黄金厅/翡翠厅/钻石厅/宝石厅): ");
        String roomName = scanner.nextLine();
        System.out.print("放映日期(年): ");
        int year = readIntInput();
        System.out.print("放映日期(月): ");
        int month = readIntInput();
        System.out.print("放映日期(日): ");
        int day = readIntInput();
        System.out.print("放映时间(时): ");
        int hour = readIntInput();
        System.out.print("放映时间(分): ");
        int minute = readIntInput();

        Movie movie = new Movie(movieName);
        ScreenRoom room = new ScreenRoom(roomName);
        LocalDateTime time = LocalDateTime.of(year, month, day, hour, minute);
        MovieSchedule schedule = new MovieSchedule(movie, room, time);

        currentUser.addMovie(cinema, schedule);
        System.out.println("电影排期添加成功！");
    }

    private static void removeMovieSchedule() {
        System.out.println("=== 删除电影排期 ===");
        currentUser.showMovieCasted(cinema);
        System.out.print("请输入要删除的电影名称: ");
        String movieName = scanner.nextLine();
        System.out.print("请输入放映厅: ");
        String roomName = scanner.nextLine();
        System.out.print("请输入放映时间(年): ");
        int year = readIntInput();
        System.out.print("请输入放映时间(月): ");
        int month = readIntInput();
        System.out.print("请输入放映时间(日): ");
        int day = readIntInput();
        System.out.print("请输入放映时间(时): ");
        int hour = readIntInput();
        System.out.print("请输入放映时间(分): ");
        int minute = readIntInput();

        LocalDateTime time = LocalDateTime.of(year, month, day, hour, minute);
        MovieSchedule toRemove = findSchedule(movieName, roomName, time);

        if (toRemove != null) {
            currentUser.removeMovie(cinema, toRemove);
            System.out.println("电影排期删除成功！");
        } else {
            System.out.println("未找到匹配的排期！");
        }
    }

    private static MovieSchedule findSchedule(String movieName, String roomName, LocalDateTime time) {
        for (MovieSchedule schedule : cinema.getMovieSchedules()) {
            if (schedule.getMovie().name.equals(movieName) &&
                    schedule.getRoom().type.equals(roomName) &&
                    schedule.getTime().equals(time)) {
                return schedule;
            }
        }
        return null;
    }

    private static void findMovie() {
        System.out.println("=== 查询电影 ===");
        System.out.print("电影名称: ");
        String movieName = scanner.nextLine();
        System.out.print("放映厅:");
        String roomName = scanner.nextLine();
        currentUser.findMovie(cinema, movieName, roomName);
    }

    private static void sellTicket() {
        System.out.println("=== 售票 ===");
        System.out.print("电影名称: ");
        String movieName = scanner.nextLine();
        System.out.print("放映厅: ");
        String roomName = scanner.nextLine();
        System.out.print("放映日期(年): ");
        int year = readIntInput();
        System.out.print("放映日期(月): ");
        int month = readIntInput();
        System.out.print("放映日期(日): ");
        int day = readIntInput();
        System.out.print("放映时间(时): ");
        int hour = readIntInput();
        System.out.print("放映时间(分): ");
        int minute = readIntInput();
        System.out.print("行号: ");
        int row = readIntInput();
        System.out.print("列号: ");
        int col = readIntInput();
        System.out.print("顾客电话: ");
        String customerPhone = scanner.nextLine();

        LocalDateTime time = LocalDateTime.of(year, month, day, hour, minute);
        currentUser.sellTicket(cinema, movieName, roomName, time, row, col, customerPhone);
    }

    private static int readIntInput() {
        while (true) {
            try {
                int input = scanner.nextInt();
                scanner.nextLine();
                return input;
            } catch (Exception e) {
                System.out.print("输入无效，请重新输入数字: ");
                scanner.nextLine();
            }
        }
    }
}

